<div class="content">

  <div class="left-content">
  
  	<div class="add-filter">
  
        <form action="#" method="post" enctype="multipart/form-data" name="addform" class="addform" id="editform">
            <div class="addform-row">
                <label>Pealkiri
                  <input name="title" type="text" class="input-field" id="title" />
                </label>
            </div>
            
            <!-- Tabhost start -->
             <div class="addform-tabhost">
               <ul>
                   
                 <?php foreach ($providers as $provider): ?>
                 <li name="<?php echo $provider->slug; ?>"><?php echo $provider->name; ?></li>
                 <?php endforeach; ?>
                 
               </ul>
             </div>	
             
            <?php foreach ($providers as $provider): ?>

             <!-- Tabhost container for #<?php echo $provider->slug; ?> start -->
             <div class="addform-tabhost-container" id="tabhost-tab-<?php echo $provider->slug; ?>">
             
           	   <div class="addform-tabhost-separator">
                    <label>
                        Kategooriad
                        <select name="categories-<?php echo $provider->slug; ?>" size="25" class="addform-tabhost-list filter-categories">
                          <?php $categories = $provider->getCategories();
                          foreach ($categories as $category): ?>
                            <option value="<?php echo $category->getID(); ?>"> <?php echo ucfirst($category->getTitle()); ?></option>
                          <?php endforeach; ?>
                        </select>
                  	</label>
                </div>	
                
               <div class="addform-tabhost-separator" id="parameters-<?php echo $provider->slug; ?>">
                   <div class="addform-message">Alustamiseks vali kategooria.</div>
               </div>	
               
             </div>	
             <!-- Tabhost container for #<?php echo $provider->slug; ?> end -->
             <?php endforeach; ?>
             
             <!-- Tabhost end -->
                          
            <div class="addform-row">
               <label>
                 <input type="checkbox" name="sendByEmail" value="1"<?php if($filter->sendByEmail) echo ' checked="checked"'; ?> /> Soovin, et uuest sisust teavitatakse mind e-posti teel
               </label>
            </div>
        
            <div class="addform-row">
                <label>Intervall
                  (sekundites) - 
                  <a href="javascript:calcAndSetInterval(1);">Iga tund</a> | <a href="javascript:calcAndSetInterval(24);">Iga päev</a> | <a href="javascript:calcAndSetInterval(7 * 24);">Kord nädalas</a> | 
                  <a href="javascript:calcAndSetInterval(3.5 * 24);">Kaks korda nädalas</a>
                  <input name="interval" type="text" class="input-field" id="interval" value="3600" />
                </label>
            </div>
            
        </form>
            
    	<!-- core.js sisaldab click meetodit. Vormi postitamiseks muuda üleval action parameetrit. (Ilmselt peaks tühi olema) -->
        <span class="button form-button focused-button" id="edit-btn" rel="<?php echo $filter->ID; ?>">Valmis</span>
        <span class="button form-button" onclick="resetForm($('form[name=addform]'));">Tühjenda väljad</span>
     </div>
     
  </div>
  
  
  <div class="right-content">
  
    <div class="right-navig-section">
    	<h2>Muuda jälgijat</h2>
    </div>
    
    <div class="right-navig-section">Vali sisuallikas ja täpsusta otsinguparameetreid.</div>
    
    <script type="text/javascript">
	
		function find_actual_params(filterObj, providerName)
		{
			for(var i = 0; i < filterObj.filterData.length; i++)
			{
				var filter = filterObj.filterData[i];
				
				if(filter.providerName == providerName)
				{
					return filter;
				}
			}
		}
		
	
		$(document).ready(function(e) {
                    
			// This comes from contoller
			var filterObj = $.parseJSON('<?php echo json_encode($filter); ?>');

			// Set title & interval
			$('input[id=title]').val(filterObj.title);
			$('input[id=interval]').val(filterObj.interval);
			
			for(var i = 0; i < filterObj.filterData.length; i++)
			{
				var filter = filterObj.filterData[i];
				
				// Get parameters and select appropriate category
				$('option[value="' + filter.categoryID + '"]').prop('selected', true);
				
				// Get default and actual params, merge and fill.
				getSupportedParameters(filter.providerName, filter.categoryID, function(parameters)
				{
					var default_params = parameters;
					var actual_params = find_actual_params(filterObj, default_params.providerName);
					
					//console.log('Setting values for provider: ' + default_params.providerName);
					
					// For each actual paremeter
					for(var actual_parameter in actual_params.parameters)
					{
						//console.log(actual_parameter);
						
						// ... find corresponding default paremeter
						for(var j = 0; j < default_params.parameters.result.length; j++)
						{
							var default_parameter = default_params.parameters.result[j];
							if(default_parameter.slug == actual_parameter)
							{
								//console.log('Found');
								
								switch (default_parameter.type)
								{
									// TYPE_SINGLE
									case 1:
										$('input[name=' + default_params.providerName + '-' + default_parameter.slug + ']').val(actual_params.parameters[actual_parameter]);
										break;
									
									// TYPE_BOOL
									case 2:
										//console.log(actual_parameter + ' is TYPE_BOOL');
										$('input[name=' + default_params.providerName + '-' + default_parameter.slug + ']').attr('checked', true);
										break;
										
									// TYPE_INTERVAL
									case 3:
										//console.log(actual_parameter + ' is TYPE_INTERVAL');
										$('input[name=' + default_params.providerName + '-min-' + default_parameter.slug + ']').val(actual_params.parameters[actual_parameter].minimum);
										$('input[name=' + default_params.providerName + '-max-' + default_parameter.slug + ']').val(actual_params.parameters[actual_parameter].maximum);
										break;
										
									// TYPE_SET
									case 4:
										//console.log(actual_parameter + ' is TYPE_SET');
										break;
								}
								
								break;
							}
						}
					}
					
					//console.log(default_params);
					//console.log(actual_params);
				});
			}
			
			// Edit
			$('#edit-btn').click(function(e) {
				updateFilter($(this).attr('rel'));
			});
			
        });
		
    </script>
	<noscript>
		<div class="right-navig-section">Viga: seadistusi ei saa näidata, luba esmalt JavaScript!</div>
	</noscript>

   
  </div>
</div>


<?php echo $main_navigation; ?>
